package com.Home.digital_life;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.annotation.SuppressLint;
import android.util.Xml;

@SuppressLint({ "UseValueOf", "UseValueOf", "UseValueOf" })
public class MediaFilePullParser {
	public static List<MediaFile> readXML(InputStream inStream) {
		XmlPullParser parser = Xml.newPullParser();
		try {
			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();
			MediaFile currentFile = null;
			List<MediaFile> medias = null;
			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:// ÎÄµµ¿ªÊŒÊÂŒþ,¿ÉÒÔœøÐÐÊýŸÝ³õÊŒ»¯ŽŠÀí
					medias = new ArrayList<MediaFile>();
					break;
				case XmlPullParser.START_TAG:// ¿ªÊŒÔªËØÊÂŒþ
					String name = parser.getName();
					if (name.equalsIgnoreCase("media")) {
						currentFile = new MediaFile();
						currentFile.setSn(new String(parser.getAttributeValue(
								null, "sn")));
					} else if (currentFile != null) {
						if (name.equalsIgnoreCase("name")) {
							currentFile.setName(new String(parser.nextText()));// Èç¹ûºóÃæÊÇTextÔªËØ,ŒŽ·µ»ØËüµÄÖµ
						} else if (name.equalsIgnoreCase("endwith")) {
							currentFile
									.setEndwith(new String(parser.nextText()));
						} else if (name.equalsIgnoreCase("lenth")) {
							currentFile
									.setLength(new Integer(parser.nextText()));
						} else if (name.equalsIgnoreCase("starring")) {
							currentFile.setStarring(new String(parser
									.nextText()));
						} else if (name.equalsIgnoreCase("size")) {
							currentFile.setSize(new Float(parser.nextText()));
						}
					}
					break;
				case XmlPullParser.END_TAG:// œáÊøÔªËØÊÂŒþ
					if (parser.getName().equalsIgnoreCase("media")
							&& currentFile != null) {
						medias.add(currentFile);
						currentFile = null;
					}
					break;
				}
				eventType = parser.next();
			}
			inStream.close();
			return medias;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}